<?php

namespace app\spadmin\library\traits;

use app\spadmin\library\Auth;


trait Backend
{
    public function add(){
        return $this->view->fetch('edit');
    }
    public function edit($ids=null){
        $row = $this->model->get(['id'=>$ids]);
        if($this->request->isPost()){
            if($row == null){
                $row = $this->model;
            }
            $data = $this->request->post('',[],'strip_tags');
   
            $result = $row->validate()->allowField(true)->save($data,$data['id']?['id'=>$data['id']]:null);
            if($result === false){
                $this->error($row->getError());
            }
            $this->success('操作成功');
        }
        $this->assign('info',$row);
        return $this->view->fetch();
    }
    public function change(){
        if($this->request->isPost()){
            $data = $this->request->post('',[],'strip_tags');
            $result = $this->model->allowField(true)->save($data,$data['id']);
            if($result === false){
                $this->error($this->model->getError());
            }
            $this->success('操作成功');
        }

    }
    public function batch(){
        if($this->request->isPost()){
            $data = $this->request->param('',[],'strip_tags');
            $result = $this->model->whereIn('id',explode(',',$data['ids']))->delete();
            if($result === false){
                $this->error($this->model->getError());
            }
            $this->success('操作成功');
        }
    }

}
